Skip to content

Conversation

manerlan
Copy link
Member

Closes #131

What

Users can find a review by the wine's name or a word in the comment.

  • API: User.reviews gets a search argument. Every word must be in the wine's name or in the comment, in any order. Like the wine search, it ignores case and accents: «rose» finds «Rosé», and «å» counts as «a», while «æ» and «ø» are letters of their own. % and _ are plain characters, and totalCount counts only the hits.
  • Page: a «Søk i dommene dine» field on /mine/dommer. It searches after a 300 ms pause, or at once on Enter.
  • URL: the search is stored as ?q=, the same name as on the search page. Search and order keep each other, and both survive a reload and «Tilbake til Mine dommer» from a wine.
  • Live region: announces the number of hits, e.g. «Viser 1 av 1 dom for «lam», nyeste først», or «Ingen dommer for «lam»».
  • No hits: the search field and the order stay, and the «no reviews yet» text is only shown when there is no search.

How

Backend

  • SQL: built in the new review-query.ts, like wine-query.ts and lists/list-query.ts. It reuses toSearchWords, escapeLike and addParam, which are now exported. list-query.ts uses the shared addParam instead of its own copy.
  • Folding: the wine's name and the comment are folded in SQL exactly as toSearchText folds the words in JavaScript (normalize(…, NFD), accents removed, lowercase), once per review with a LATERAL. I compared the two in PostgreSQL.
  • No migration and no new index: review_user_id finds the user's reviews, and only those are compared.

Frontend

  • useDebouncedUrlText: a new shared hook for a search field whose search lives in the URL. The search page's useSearchText now uses it, with the same behaviour as before.
  • Not useDebouncedValue: main now also has useDebouncedValue, used for the suggestions. It debounces a value, not a write to the URL. I did not build on it, as a lagging value could write an old search back into the URL after the back button changes it.
  • Field in state: the text in the field is in state, so no key press is lost.
  • Cache: keyArgs for User.reviews is now ['order', 'search']. Without a search, the reviews still share the cache with «Min side».
  • Back link: the wine page's back link accepted only [a-z0-9=&-] in the query, so a search like ?q=r%C3%B8d+lam fell back to «Utforsk alle viner». It now accepts what an encoded query holds, but still no /, : or <. The tests for invalid targets are extended.

Fix of a race that was already on main

On a busy machine, search.spec.ts turned «pinot» into «pnot». The hook's own URL change could arrive late, after the user had typed on, and was then taken for a change from outside, which reset the field. This was already in useSearchText on main. The hook now remembers what it wrote, and only follows URL changes from outside (a link, the back button). useDebouncedUrlText.test.tsx reproduces the race by holding the URL write back, with fake timers, so it does not depend on machine speed.

Tests

  • review-query.test.ts (no database):
    • the SQL with and without a search, in both orders
    • one condition per word, in both the page and the count
    • folded and de-duplicated words, % and _ escaped
    • no search text in the SQL itself
    • at most 10 words
  • review-api.db.test.ts:
    • a word in a comment, ignoring case
    • the wine name with and without accents
    • every word in any order, also one in the name and one in the comment
    • % and _ as plain characters, and a blank search
    • never another user's reviews
    • paging and counts of hits in both orders
  • MyReviewsPage.test.tsx:
    • the search from the URL, and no hits
    • search kept when the order changes, and «Last inn flere» within a search
    • axe
    • typing with fake timers: one request after the pause and none for «l» or «la» on the way, Enter, and an emptied field served from the cache
  • useDebouncedUrlText.test.tsx:
    • the URL changes only after the pause
    • typed text kept while the hook's own URL change is on the way
    • the field follows changes from outside
    • a trailing space is kept
  • apollo.test.ts: each search is cached apart, and the reviews without a search are stored where «Min side» finds them.
  • WinePage.test.tsx: the back link with a search in the URL, and more invalid targets.
  • e2e/profile.spec.ts: search on «Mine dommer», kept in the URL, after a reload and back from a wine, with no axe violations. It uses fill, not arrow keys, so it runs the same on macOS. The fake API can now search the reviews.

Docs

  • docs/api.md: search on User.reviews with an example query, how the text is folded, and useDebouncedUrlText in the «Min side» table.
  • docs/manual-testing.md: five new steps.
  • README: «Last updated» (this also removes a duplicate line from main), and the «Min side» row.

How to test

  1. Checks: npm run check, TEST_DATABASE_URL=… npm test in backend/, and npx playwright test e2e/profile.spec.ts e2e/search.spec.ts in frontend/.
  2. Log in as demo (after npm run db:seed:demo) and open /mine/dommer.
  3. Search for LAM. It finds «… perfekt til lam.», ?q=LAM is in the URL, and focus stays in the field.
  4. Search for a word no review has. «Ingen dommer for «…»» is shown, and the field and the order stay.
  5. Choose «Eldste først» and reload. Both the search and the order are kept.
  6. Open a wine, then «Tilbake til Mine dommer». The same search and order.
  7. On /sok: the search works as before, also with fast typing.

Review notes

  • No migration. Run npm ci --prefix frontend after pulling main (Downshift).
  • Flaky tests from main: landing.spec.ts (from main, not touched here) timed out a few times under heavy load: the axe test and the search from the landing page on mobile. With --workers=1 --repeat-each=10 it passed every time.
  • To try in the API: in Git Bash on Windows, curl may not send ø as UTF-8. Use \u00f8 in the query, or test in the browser.
  • **Also makes search.spec.ts «sender bare ett søk …» independent of machine speed with page.clock. It sent an extra search when a key took more than 300 ms under load.
  • **npm run check and npm run e2e might give a failed test, (happened to me), but no test should fail individually. Heavy load on the machine can cause tests to fail or timeout. If a test doesn't pass, check individually

AI

Claude used to generate code and tests based on prompts. Implemented and tested manually. Claude used to create initial PR text for easy summary, review and added to manually afterwards.

@manerlan manerlan requested a review from a team as a code owner November 26, 2026 21:22
@manerlan manerlan linked an issue Nov 26, 2026 that may be closed by this pull request
4 tasks
Copy link
Member

@oscarba oscarba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and looks good

@oscarba oscarba merged commit a1b541b into main Nov 26, 2026
@oscarba oscarba deleted the 131-search-reviews branch November 26, 2026 21:59
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search in your own reviews
2 participants